/**
 * 
 */
package jmine.tec.report.cell.impl;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.Date;

import jmine.tec.report.cell.DataCell;

/**
 * {@link DataCell} para valores booleanos
 * 
 * @author takeshi
 */
public final class BooleanDataCell implements DataCell, Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -426757161322695551L;

    private final boolean value;

    public static final BooleanDataCell TRUE = new BooleanDataCell(true);

    public static final BooleanDataCell FALSE = new BooleanDataCell(false);

    /**
     * Returns the {@link BooleanDataCell} corresponding to the given value
     * 
     * @param value the boolean value
     * @return a {@link BooleanDataCell}
     */
    public static BooleanDataCell valueOf(final boolean value) {
        return value ? TRUE : FALSE;
    }

    /**
     * @param value boolean
     */
    private BooleanDataCell(final boolean value) {
        super();
        this.value = value;
    }

    /**
     * @return {@link Date}
     * @see br.com.maps.util.report.cell.DataCell#toDate()
     */
    public Date toDate() {
        return null;
    }

    /**
     * @return double
     * @see br.com.maps.util.report.cell.DataCell#toDouble()
     */
    public Double toDouble() {
        return this.value ? Double.valueOf(1) : Double.valueOf(0);
    }

    /**
     * @param dateFormat the date format
     * @param doubleFormat the double format
     * @param currencyFormat the currency format
     * @return string
     * @see br.com.maps.util.report.cell.DataCell#toString(java.text.DateFormat, java.text.NumberFormat, java.text.NumberFormat)
     */
    public String toString(final DateFormat dateFormat, final NumberFormat doubleFormat,
            final NumberFormat currencyFormat) {
        return String.valueOf(this.value);
    }

    /**
     * @param o other
     * @return DataCell
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     */
    public int compareTo(final DataCell o) {
        if (this.equals(o)) {
            return 0;
        }
        if (o instanceof BooleanDataCell) {
            if (this.value) {
                return 1;
            } else {
                return -1;
            }
        }
        return 1;
    }

    /**
     * @return int
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        return this.value ? -1 : 0;
    }

    /**
     * @param obj the other object
     * @return boolean
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(final Object obj) {
        return this == obj; // identity equality because theres only 2 instances
    }

    /**
     * @return the value
     */
    public boolean isValue() {
        return this.value;
    }

    /**
     * @return String
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return String.valueOf(this.value);
    }

    /**
     * Implementation of {@link Serializable} to provide custom resolving mechanism while desserializing an object instance
     * 
     * @return {@link BooleanDataCell}
     */
    protected Object readResolve() {
        return this.value ? TRUE : FALSE;
    }

}
